package org.csu.question.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.csu.question.entity.po.Questions;

import java.util.List;

@Mapper
public interface QuestionMapper extends BaseMapper<Questions> {

    /**
     * 根据知识点ID查询题目
     */
    @Select("SELECT * FROM questions WHERE knowledge_point_id = #{knowledgePointId}")
    List<Questions> selectByKnowledgePointId(Long knowledgePointId);

    /**
     * 根据标签查询题目
     */
    @Select("SELECT q.* FROM questions q " +
            "LEFT JOIN knowledge_points kp ON q.knowledge_point_id = kp.id " +
            "WHERE kp.tag = #{tag}")
    List<Questions> selectByTag(String tag);

    /**
     * 根据难度查询题目
     */
    @Select("SELECT * FROM questions WHERE difficulty = #{difficulty}")
    List<Questions> selectByDifficulty(String difficulty);

    /**
     * 查询用户未答过的题目
     */
    @Select("SELECT q.* FROM questions q " +
            "WHERE q.id NOT IN (" +
            "SELECT ar.question_id FROM answer_records ar WHERE ar.user_id = #{userId}" +
            ")")
    List<Questions> selectUnansweredQuestions(Long userId);

    /**
     * 根据知识点ID查询用户未答过的题目
     */
    @Select("SELECT q.* FROM questions q " +
            "WHERE q.knowledge_point_id = #{knowledgePointId} " +
            "AND q.id NOT IN (" +
            "SELECT ar.question_id FROM answer_records ar WHERE ar.user_id = #{userId}" +
            ")")
    List<Questions> selectUnansweredQuestionsByKnowledgePoint(Long knowledgePointId, Long userId);
}
